require 'builder'

number = 200
width = 300
height = 1400
gap = 200

character_range = "F"
character_starting_number = 0

ttglyph = Builder::XmlMarkup.new(:indent => 2 )
glyphorder = Builder::XmlMarkup.new(:indent => 2 )
cmap = Builder::XmlMarkup.new(:indent => 2 )
hmtx = Builder::XmlMarkup.new(:indent => 2 )
post = Builder::XmlMarkup.new(:indent => 2 )


cmap.cmap_format_4(:platformID => "3", :platEncID => "1", :version=> "0") do 
glyphorder.GlyphOrder do 
hmtx.htmx do 
post.post do 
post.extranames do

offset = 0
for style in ["positives","negatives","middles","more_positives","more_negatives"]
  
  for i in 0..(number - 1)
    char_name = "uni" + character_range.upcase + sprintf("%03X", i + offset)
    map_code = "0x" + character_range.downcase + sprintf("%03x", i + offset)
    
    x1,x2,x3,x4,y1,y2,y3,y4 = 0,0,0,0,0,0,0,0
    
    case style
    when "positives"
      x1, y1 = 0,0
      x2, y2 = x1, i * height / number
      x3, y3 = width, y2
      x4, y4 = x3, y1
    when "negatives"
      x1, y1 = 0,height
      x2, y2 = width, y1
      x3, y3 = x2, height - (i * height / number)
      x4, y4 = x1, y3
    when "middles"
      case i
      when 0..99
        x1, y1 = 0,i * height / number
        x2, y2 = x1, height / 2
        x3, y3 = width, y2
        x4, y4 = x3, y1
      when 100..199
        x1, y1 = 0, height / 2
        x2, y2 = x1, i * height / number
        x3, y3 = width, y2
        x4, y4 = x3, y1
      end
    when "more_positives"
      case i
        when 0..66
          x1, y1 = 0,i * height / number
          x2, y2 = x1, (height / 3).to_i
          x3, y3 = width, y2
          x4, y4 = x3, y1
        when 67..199
          x1, y1 = 0, (height / 3).to_i
          x2, y2 = x1, i * height / number
          x3, y3 = width, y2
          x4, y4 = x3, y1
        end
    when "more_negatives"
        case i
		when 0..133
          x1, y1 = 0,i * height / number
          x2, y2 = x1, (2 * height / 3).to_i
          x3, y3 = width, y2
          x4, y4 = x3, y1
        when 134..199
          x1, y1 = 0, (2 * height / 3).to_i
          x2, y2 = x1, i * height / number
          x3, y3 = width, y2
          x4, y4 = x3, y1
        end
    end

    xMin = [x1,x2,x3,x4].sort!
    yMin = [y1,y2,y3,y4].sort!
    
    ttglyph.TTGlyph(:name => char_name, :xMin => xMin[0], :yMin => yMin[0], :xMax=> xMin[3], :yMax => yMin[3]) do
      ttglyph.contour do
        ttglyph.pt(:x => x1, :y => y1, :on => "1")
        ttglyph.pt(:x => x2, :y => y2, :on => "1")
        ttglyph.pt(:x => x3, :y => y3, :on => "1")
        ttglyph.pt(:x => x4, :y => y4, :on => "1")
      end

      ttglyph.instructions do 
        ttglyph.assembly 
      end
    end
    
    cmap.map(:code => map_code, :name => char_name)
    glyphorder.GlyphID(:id => "#{i+offset}", :name => char_name)
    
    hmtx.mtx(:name => char_name, :width => width+gap, :lsb => "0")
    post.psName(:name =>char_name)
    


  #  puts char_name
  #  puts "#{x1} #{y1}" 
  #  puts "#{x2} #{y2}"
  #  puts "#{x3} #{y3}" 
  #  puts "#{x4} #{y4}"
  #  puts ""
  end
  offset += number
end

end
end
end
end
end

xml_file = File.new("xml.txt", "w")
xml_file.print glyphorder.target!
xml_file.print "\n\n\n"
xml_file.print cmap.target!
xml_file.print "\n\n\n"
xml_file.print ttglyph.target!
xml_file.print "\n\n\n"
xml_file.print hmtx.target!
xml_file.print "\n\n\n"
xml_file.print post.target!
xml_file.close